package com.crm.service.impl.activityManager;

import com.alibaba.fastjson.JSONArray;
import com.crm.dao.mapper.activityManager.CustomerProductGiftActivityMapper;
import com.crm.dao.mapper.customMapper.activityManagerCustomMapper.CustomerProductGiftActivityCustomMapper;
import com.crm.model.entity.activityManager.CustomerProductGiftActivity;
import com.crm.model.entity.activityManager.CustomerProductGiftActivityExample;
import com.crm.model.qo.activityManager.CustomerProductGiftActivityQO;
import com.crm.model.vo.activityManager.CustomerProductGiftActivityContractVO;
import com.crm.model.vo.activityManager.CustomerProductGiftActivityVO;
import com.crm.model.vo.activityManager.CustomerProductGiftDetailVO;
import com.crm.service.activityManager.CustomerProductGiftActivityDbService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author luojie
 * @date 2021/3/1 14:49
 */
@Service
public class CustomerProductGiftActivityDbServiceImpl implements CustomerProductGiftActivityDbService {
    @Autowired
    private CustomerProductGiftActivityMapper customerProductGiftActivityMapper;

    @Autowired
    private CustomerProductGiftActivityCustomMapper productGiftActivityCustomMapper;

    /**
     * 活动管理--赠品活动列表
     * @param productGiftActivityQO ---赠品列表查询条件
     * @param orderStr              ---赠品列表排序
     * @author yincuimei
     * @return
     */
    @Override
    public List<CustomerProductGiftActivityVO> selectCustomerProductGiftActivityVOList(CustomerProductGiftActivityQO productGiftActivityQO, String orderStr) {
        return productGiftActivityCustomMapper.selectCustomerProductGiftActivityVOList(productGiftActivityQO,orderStr);
    }

    /**
     * 赠品活动列表--(赠品活动上架时判断)根据赠品活动ID查询活动是否在有效期内
     * @param productGiftActivityId  ---赠品活动ID
     * @param nowDate                ---当前时间
     * @Author yincuimei
     * @return
     */
    @Override
    public Boolean selectGiftActivityIsInValidityPeriod(String productGiftActivityId, String nowDate) {
        return productGiftActivityCustomMapper.selectGiftActivityIsInValidityPeriod(productGiftActivityId,nowDate) > 0;
    }

    /**
     * 赠品活动列表--赠品活动上/下架操作
     * @param productGiftActivityId    ---赠品活动主键
     * @param giftShelfStatus          ---赠品活动上下架状态(0:上架;1:下架)
     * @param accountId                ---当前操作人账号ID
     * @Author yincuimei
     * @return
     */
    @Override
    public Boolean updateGiftActivityShelfStatus(String productGiftActivityId, String giftShelfStatus, String accountId) {
        CustomerProductGiftActivity productGiftActivity = new CustomerProductGiftActivity();
        productGiftActivity.setProductGiftActivityId(productGiftActivityId);//赠品活动ID
        productGiftActivity.setGiftShelfStatus(giftShelfStatus);//上下架状态
        productGiftActivity.setUpdateTm(new Date());//更新时间
        productGiftActivity.setUpdatePeopleId(accountId);//操作人账号ID
        return customerProductGiftActivityMapper.updateByPrimaryKeySelective(productGiftActivity) > 0;
    }

    /***
     * 赠品活动列表--查看产品类赠品活动(根据赠品活动ID查询赠品活动)关联会员产品
     * @param productGiftActivityId ---赠品活动ID
     * @Author yincuimei
     * @return
     */
    @Override
    public CustomerProductGiftDetailVO selectMemberProductCategoryGiftActivityById(String productGiftActivityId) {
        return productGiftActivityCustomMapper.selectMemberProductCategoryGiftActivityById(productGiftActivityId);
    }

    /***
     * 赠品活动列表--查看产品类赠品活动(根据赠品活动ID查询赠品活动)关联非会员产品
     * @param productGiftActivityId ---赠品活动ID
     * @Author yincuimei
     * @return
     */
    @Override
    public CustomerProductGiftDetailVO selectNonMemberProductCategoryGiftActivityById(String productGiftActivityId) {
        return productGiftActivityCustomMapper.selectNonMemberProductCategoryGiftActivityById(productGiftActivityId);
    }

    /***
     * 赠品活动列表--查看价格类赠品活动(根据赠品活动ID查询赠品活动)
     * @param productGiftActivityId ---赠品活动ID
     * @Author yincuimei
     * @return
     */
    @Override
    public CustomerProductGiftDetailVO selectPriceCategoryGiftActivityById(String productGiftActivityId) {
        CustomerProductGiftActivity productGiftActivity = customerProductGiftActivityMapper.selectByPrimaryKey(productGiftActivityId);
        CustomerProductGiftDetailVO productGiftDetailVO = new CustomerProductGiftDetailVO();
        if(productGiftActivity != null){
            productGiftDetailVO.setProductGiftActivityId(productGiftActivity.getProductGiftActivityId());//赠品活动ID
            productGiftDetailVO.setGiftActivityName(productGiftActivity.getGiftActivityName());//赠品活动名称
            productGiftDetailVO.setProductName("");//产品名称
            productGiftDetailVO.setGiftActivityPriceStart(productGiftActivity.getGiftActivityPriceStart());//赠品活动价格起始价
            productGiftDetailVO.setGiftActivityPriceEnd(productGiftActivity.getGiftActivityPriceEnd());//赠品活动价格截止价
            productGiftDetailVO.setGiftPeriodValidityStart(productGiftActivity.getGiftPeriodValidityStart());//赠品活动有效期起始时间
            productGiftDetailVO.setGiftPeriodValidityEnd(productGiftActivity.getGiftPeriodValidityEnd());//赠品活动有效期截止时间
            productGiftDetailVO.setGiftShelfStatus(productGiftActivity.getGiftShelfStatus());//赠品活动上/下架状态
        }
        return productGiftDetailVO;
    }

    /**
     * 赠品列表--删除赠品
     * @param customerProductGiftActivity
     * @Author yincuimei
     * @return
     */
    @Override
    public Boolean deleteCustomerProductActivityById(CustomerProductGiftActivity customerProductGiftActivity) {
        return customerProductGiftActivityMapper.updateByPrimaryKeySelective(customerProductGiftActivity) > 0;
    }

    @Override
    public Boolean insertCustomerProductGiftActivity(CustomerProductGiftActivity customerProductGiftActivity) {
        Boolean result = false;
        int num = customerProductGiftActivityMapper.insertSelective(customerProductGiftActivity);
        if(num>0){
            result = true;
        }
        return result;
    }

    @Override
    public Boolean selectCustomerProductGiftIsRepeat(String productGiftName,String productGiftId) {
        Boolean result = false;
        CustomerProductGiftActivityExample customerProductGiftActivityExample = new CustomerProductGiftActivityExample();
        CustomerProductGiftActivityExample.Criteria criteria = customerProductGiftActivityExample.createCriteria();
        criteria.andGiftActivityNameEqualTo(productGiftName);
        if(!StringUtils.isBlank(productGiftId)){
            criteria.andProductGiftActivityIdNotEqualTo(productGiftId);
        }
        List<CustomerProductGiftActivity> customerProductGiftActivityList =  customerProductGiftActivityMapper.selectByExample(customerProductGiftActivityExample);
        if(customerProductGiftActivityList.size()>0){
            result = true;
        }
        return result;
    }

    @Override
    public CustomerProductGiftActivity selectProductGiftActivityById(String productGiftActivityId) {
        CustomerProductGiftActivity customerProductGiftActivity = customerProductGiftActivityMapper.selectByPrimaryKey(productGiftActivityId);
        return customerProductGiftActivity;
    }

    @Override
    public Boolean editProductGiftActivity(CustomerProductGiftActivity customerProductGiftActivity) {
        Boolean result = false;
        int num = customerProductGiftActivityMapper.updateByPrimaryKeySelective(customerProductGiftActivity);
        if(num>0){
            result = true;
        }
        return result;
    }

    @Override
    public List<CustomerProductGiftActivityContractVO> selectProductGiftListByProductIdAndPrice(JSONArray selectProductGiftListByProductIdList, BigDecimal price) {
        List<CustomerProductGiftActivityContractVO> customerProductGiftActivityList = productGiftActivityCustomMapper.selectProductGiftListByProductIdAndPrice(selectProductGiftListByProductIdList,price);
        return customerProductGiftActivityList;
    }
}
